/**
 * Created by BIYY on 2017-11-17.
 */
import {Loading} from 'element-ui';
import axios from 'axios';
import config from '../../../config/index';
import urlUtil from '../url/index';
import I18N from '../../../config/i18n/index';

let loadingInstance = null;
let request = {};
/**
 * ajax请求失败
 * @param msg
 */
request.ajaxError = (msg, timeOut, locate) => {
    if (msg) {
        setTimeout(() => {
            if (typeof  msg === 'string') {
                message.error(msg);
            } else {
                message.error(JSON.stringify(msg));
            }
        }, 20);
    } else {
        message.error(I18N[locate].networkError);
    }
    Loading.close();
    clearTimeout(timeOut);
};
/**
 * ajax
 * @param opt
 */
request.ajax = (opt, locate = 'zh_CN') => {
    let option = {};
    let setTime = {};
    let time = new Date() * 1;
    let showLoading = true; //是否出現loading
    let loadingTime = config.loadingTime || 500; //出现loading时间
    let method = opt.api.method || config.requestType.get;
    let url = opt.api.url;
    let headers = opt.headers || opt.api.headers || {};
    if (!url) {
        request.ajaxError(I18N[locate].emptyUrl, setTime[time], locate);
        return false;
    }
    if (opt.api && opt.api.loadingTime) {
        loadingTime = opt.api.loadingTime;
    }
    setTime[time] = setTimeout(() => { // 超过一定時間后出现加载条
        if (showLoading) {
            loadingInstance = Loading.service({fullscreen: false});
        }
    }, loadingTime);
    option.headers = headers;
    option.method = method;
    option.url = url;
    if (opt.api && opt.api.inject) { //自定义注入器
        option = opt.api.inject(option);
    }
    if (method === config.requestType.get) { //get请求
        if (opt.data) {
            option.params = opt.data;
        }
    } else { //post请求
        if (opt.data) {
            option.data = opt.data;
        }
    }
    option.params = option.params || opt.params || {};
    if (opt.api.download) {
        location.href = urlUtil.urlEncode(opt.api.url, opt.data);
        hide();
        clearTimeout(setTime[time]);
        return;
    }
    return new Promise((resolve, reject) => {
        axios(option)
            .then((response) => {
                request.render(response, opt, setTime, time, resolve, locate);
               loadingInstance.close();
                clearTimeout(setTime[time]);
            }).catch((error) => {
            console.log(error.response);
            let response = error.response;
            request.render(response, opt, setTime, time, resolve, locate);
           loadingInstance.close();
            clearTimeout(setTime[time]);
        });
    });
};
request.render = (response, opt, setTime, time, resolve, locate) => {
    console.log(opt.api);
    if (response) {
        if (opt.api && opt.api.intercept) { //自定义状态码拦截器
            opt.api.intercept(response.data, resolve, (msg) => {
                request.ajaxError(msg, setTime[time], locate);
            });
        } else if (response.status === 503) {
            request.ajaxError(response.statusText, setTime[time], locate);
        } else {
            resolve(response.data);
        }
    } else {
        request.ajaxError(I18N[locate].networkError, setTime[time], locate);
    }
};
export default request;